package com.example.learnspringboot05web.config;

import com.example.learnspringboot05web.filter.MyFilter;
import com.example.learnspringboot05web.listener.Mylistener;
import com.example.learnspringboot05web.servlet.Myservlet;
import org.springframework.boot.web.embedded.jetty.ConfigurableJettyWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.lang.management.MemoryUsage;
import java.lang.reflect.Array;
import java.util.Arrays;

@Configuration
public class MyServeltConfig {

    //注册三大组件
    @Bean
    public ServletRegistrationBean myServlet() {
        //http://localhost:8084/crud/myServlet
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new Myservlet(), "/myServlet");
        return servletRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new MyFilter());
        filterRegistrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet"));
        return filterRegistrationBean;
    }

    @Bean
    public ServletListenerRegistrationBean mylistener(){
        ServletListenerRegistrationBean<Mylistener> registrationBean = new ServletListenerRegistrationBean<Mylistener>(new Mylistener());
        return registrationBean;
    }


    //配置嵌入式的servlet容器

    /*

        https://www.jianshu.com/p/b973476ccfd6   https://blog.csdn.net/qq_43843951/article/details/108049897
        Spring Boot2.0以上版本EmbeddedServletContainerCustomizer被WebServerFactoryCustomizer替代

     */
    @Bean
    public WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory> webServerFactoryWebServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableJettyWebServerFactory>() {
            //定制嵌入式的servlet容器相关规则

            @Override
            public void customize(ConfigurableJettyWebServerFactory factory) {
                factory.setPort(8082);
            }
        };
    }
}
